package com.newcoder.config;

import com.newcoder.controller.filter.CustomizeFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {


    @Bean
    public FilterRegistrationBean customizeFilter01() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        CustomizeFilter customizeFilter = new CustomizeFilter();

        //注入自定义过滤器
        filterRegistrationBean.setFilter(customizeFilter);
        //配置过滤规则
        filterRegistrationBean.addUrlPatterns("/*");
        //设置init参数
        filterRegistrationBean.addInitParameter("name","customizeFilter");
        //设置过滤器名称
        filterRegistrationBean.setName("myFilter1");
        //执行次序
        filterRegistrationBean.setOrder(1);

        return filterRegistrationBean;
    }

    /**
     * 可以定义多个不同的过滤器，然后设置执行顺序
     *     @Bean
     *     public FilterRegistrationBean customizeFilter02() {
     *         FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
     *         CustomizeFilter customizeFilter = new CustomizeFilter();
     *
     *         filterRegistrationBean.setFilter(customizeFilter02);
     *         //配置过滤规则
     *         filterRegistrationBean.addUrlPatterns("/*");
     *         //设置init参数
     *         filterRegistrationBean.addInitParameter("name","customizeFilter");
     *         //设置过滤器名称
     *         filterRegistrationBean.setName("myFilter1");
     *         //执行次序
     *         filterRegistrationBean.setOrder(2);
     *
     *         return filterRegistrationBean;
     *     }
     * */
}